package com.robotserver.orders.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.DigestUtils;

import java.util.TreeMap;
import java.util.UUID;

/**
 * 穿山甲API接口工具
 */
public class CsjApiUtil {
    private final static Logger logger = LoggerFactory.getLogger(CsjApiUtil.class);
    private final static String APP_ID = "5455119";
    private final static String SECURE_KEY = "105ddc5b3a8a662b516b546a130add8375c9f4a7dda83fb844642f0a5c2cc527";
    private final static String SIGN_TYPE = "MD5";

    public static class RequestBody {
        private String app_id;
        private String sign_type;
        private String version = "1";
        private long timestamp;
        private String req_id;
        private String data;
        private String sign;

        public RequestBody(String app_id, String sign_type, long timestamp, String req_id, String data) {
            this.app_id = app_id;
            this.sign_type = sign_type;
            this.timestamp = timestamp;
            this.req_id = req_id;
            this.data = data;
        }

        public String getApp_id() {
            return app_id;
        }

        public void setApp_id(String app_id) {
            this.app_id = app_id;
        }

        public String getSign_type() {
            return sign_type;
        }

        public void setSign_type(String sign_type) {
            this.sign_type = sign_type;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        public long getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(long timestamp) {
            this.timestamp = timestamp;
        }

        public String getReq_id() {
            return req_id;
        }

        public void setReq_id(String req_id) {
            this.req_id = req_id;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

        public String getSign() {
            return sign;
        }

        public void setSign(String sign) {
            this.sign = sign;
        }
    }

    private static String getSign(String secret, RequestBody req) {
        String signStr = String.format("app_id=%s&data=%s&req_id=%s&timestamp=%d%s", req.getApp_id(), req.getData(),
                req.getReq_id(), req.getTimestamp(), secret);
        return DigestUtils.md5DigestAsHex(signStr.getBytes());
    }

    public static RequestBody getParams(String appId, String secret, TreeMap<String,Object> data){
        int timestamp = DateUtil.getNowTime();
        RequestBody requestBody = new RequestBody(appId, SIGN_TYPE, timestamp, UUID.randomUUID().toString(), JSON.toJSONString(data, SerializerFeature.MapSortField));
        requestBody.setSign(getSign(secret, requestBody));
        return requestBody;
    }


}